package uk.org.landeg.euler.problems.framework;

import java.util.Date;

public class ProblemLauncher {
	private final AbstractProblem problem;
	
	public ProblemLauncher(AbstractProblem problem) {
		System.out.println(String.format("Running problem %d\n----------------\n%s",problem.getProblemNumber(), problem.getDescription()));
		this.problem = problem;
	}
	
	public void solve () {
		new Thread(new Runnable() {
			public void run() {
				Object solution;
				Date start, end;
				start = new Date();
				System.out.println("Starting at " + start.toString());
				
				solution = problem.solve();
				System.out.println("====== Solution =====");
				System.out.println("|  " + solution.toString() + "  |");
				System.out.println("=====================");
				end = new Date();
				System.out.println("Problem solved at " + end.toString());
				long millis = end.getTime() - start.getTime();
				
				if (problem.notes.size() > 0) {
					System.out.println("\n=====Additional notes");
					for (String line : problem.notes) {
						System.out.println(line);
					}
					System.out.println(" ----------- ");
				}
				System.out.println("Problem " + problem.getProblemNumber() + " solved in " + millis + "ms");				
			}
		}).start();
	}
	
	public void hammer (int counts) {
		long start, end;
		System.out.println("Hammering problem;");
		start = System.currentTimeMillis();
		for (int idx = 0 ; idx < counts ; idx++) {
			problem.solve();
		}
		end = System.currentTimeMillis();
		System.out.println("Hammering finished : time taken " + (end-start) + "ms");
		System.out.println("Average per run" + (end-start)/counts + "ms");
	}
	
}
